#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :2.0.5 互斥锁的使用.py
# @Time      :2020/7/13 18:27
# @Author    :亮亮
# @说明       :
# @总结       :
import threading
import time

# 创建全局变量
g_num = 0

# 创建一个互斥锁,默认是没有上锁的
mutex = threading.Lock()


def name_1(num):
    global g_num

    # 上锁
    mutex.acquire()

    for i in range(num):
        g_num += 1

    # 解锁
    mutex.release()
    print(g_num)


def name_2(num):
    global g_num

    # 上锁
    mutex.acquire()

    for i in range(num):
        g_num += 1

    # 解锁
    mutex.release()
    print(g_num)


def main():
    # 创建循环次数
    num = int(input("输入循环次数："))

    # 创建多线程
    t_1 = threading.Thread(target=name_1, args=(num,))
    t_2 = threading.Thread(target=name_2, args=(num,))

    # 开启多线程
    t_1.start()
    t_2.start()


if __name__ == '__main__':
    main()